/**
 * <copyright>
 * Copyrigth 2007 Aurelien Moreau
 *
 * This file is part of ProcessShop.
 *
 * ProcessShop is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * ProcessShop is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with ProcessShop; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 * </copyright>
 *
 * $Id$
 */
package net.yienyien.process.folder.stdPatterns.impl;

import net.yienyien.process.bpd.BPDComponent;
import net.yienyien.process.bpd.BPDObject;
import net.yienyien.process.bpd.BpdPackage;
import net.yienyien.process.folder.stdPatterns.Sequence;
import net.yienyien.process.folder.stdPatterns.StdPatternsPackage;

import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Sequence</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * </p>
 *
 * @generated
 */
public class SequenceImpl extends StdBPDPatternImpl implements Sequence {
	
	public static final String NAME="Sequence";
	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	protected SequenceImpl() {
		super(NAME, null, null, BpdPackage.eINSTANCE.getTask(), BpdPackage.eINSTANCE.getIntermediateEvent());
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return StdPatternsPackage.Literals.SEQUENCE;
	}

	@Override
	public EList<BPDObject> getContent(BPDObject source, BPDObject sink) {
		
		//System.out.println("<Sequence>");
		//System.out.println(source.getFrom());
		//System.out.println(sink.getFrom());
		
		EList<BPDObject> set=new BasicEList<BPDObject>();
		
		BPDObject current;
		
		if(!inOutOne(source) || !inOutOne(sink)) {
			return null;
		}
		
		//System.out.println("1.");
		
		current=source.getInputs().iterator().next().getInput();
		if((BpdPackage.eINSTANCE.getTask().isInstance(current) ||
				BpdPackage.eINSTANCE.getIntermediateEvent().isInstance(current)) 
			&& inOutOne(current)) { // not max
			return null;
		}
		//System.out.println("2.");
		
		current=sink.getOutputs().iterator().next().getOutput();
		if((BpdPackage.eINSTANCE.getTask().isInstance(current) || 
				BpdPackage.eINSTANCE.getIntermediateEvent().isInstance(current)) 
			&& inOutOne(current)) { // not max
			return null;
		}
		
		//System.out.println("3.");
		
		current=source.getOutputs().iterator().next().getOutput();
		
		while(!current.equals(sink)) {
			set.add(current);
			if(!inOutOne(current)) {
				return null;
			}
			current=current.getOutputs().iterator().next().getOutput();
		}
		
		//System.out.println("4.");

		return set;
	}

	@Override
	public boolean verify(BPDComponent component) {
		return true; // TODO
	}
	
	private boolean inOutOne(BPDObject o) {
		return o.getInputs().size()==1 && o.getOutputs().size()==1;
	}


} //SequenceImpl
